﻿using FbHelperWP7NonUi.BookStorage.DropBoxStorage;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Threading;

namespace FbHelperNonUiTests
{
    [TestClass()]
    public sealed class AsyncStorageHelperTest
    {
        private const string email = "igor.manushin@gmail.com";
        private const string password = "999999";
        private const string wrongPassword = "123";

        private const string filePath = "/Getting Started.pdf";

        [TestMethod()]
        public void SuccessStartLoginTest()
        {
            bool isEnded = false;

            AsyncStorageHelper target = new AsyncStorageHelper();

            target.BeginOpenStorage( email, password, ( result ) =>
            {
                isEnded = true;
                Assert.IsTrue( result.IsSuccess );
            } );

            while (!isEnded)
                Thread.Sleep( 100 );
        }

        [TestMethod()]
        public void WrongStartLoginTest()
        {
            bool isEnded = false;

            AsyncStorageHelper target = new AsyncStorageHelper();

            target.BeginOpenStorage( email, wrongPassword, ( result ) =>
            {
                isEnded = true;
                Assert.IsNotNull( result );                
                Assert.IsFalse( result.IsSuccess );
            } );

            while (!isEnded)
                Thread.Sleep( 100 );
        }

        [TestMethod]
        public void BeginGetDirectoryEntryTest()
        {
            bool isEnded = false;

            AsyncStorageHelper target = new AsyncStorageHelper();

            target.BeginOpenStorage( email, password, ( result ) =>
            {
                isEnded = true;
            } );

            while (!isEnded)
                Thread.Sleep( 100 );

            isEnded = false;

            target.BeginGetDirectoryEntry( "/", ( result ) =>
            {
                Assert.IsNotNull( result );
                Assert.IsNotNull( result.Directories );
                Assert.IsNotNull( result.Files );
                Assert.IsTrue( result.IsSuccess );
                isEnded = true;
            } );

            while (!isEnded)
                Thread.Sleep( 100 );
                    }
   
        [TestMethod]
        public void BeginGetFileEntryTest()
        {
            bool isEnded = false;

            AsyncStorageHelper target = new AsyncStorageHelper();

            target.BeginOpenStorage( email, password, ( result ) =>
            {
                isEnded = true;
            } );

            while (!isEnded)
                Thread.Sleep( 100 );

            isEnded = false;

            target.BeginGetFileEntry( filePath, ( result ) =>
            {
                Assert.IsNotNull( result );
                Assert.IsNotNull( result.GetFileEntry() );
                Assert.IsTrue( result.IsSuccess );

                isEnded = true;
            } );

            while (!isEnded)
                Thread.Sleep( 100 );
        }
 }
}
